home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 3 / Amiga Format CD03 (1996-07-04)(Future Publishing)(GB)(Track 1 of 6)[!][issue 1996-08].iso / comms / netsoftware / archie38_1.lha / archie-1.4 / procquery.c < prev    next >
C/C++ Source or Header  |  1995-01-05  |  7KB  |  292 lines

  1. /*
  2.  * procquery.c : Routines for processing results from Archie
  3.  *
  4.  * Originally part of the Prospero Archie client by Cliff Neuman (bcn@isi.edu).
  5.  * Modified by Brendan Kehoe (brendan@cygnus.com).
  6.  * Re-modified by George Ferguson (ferguson@cs.rochester.edu).
  7.  *
  8.  * Copyright (c) 1991 by the University of Washington
  9.  *
  10.  * For copying and distribution information, please see the file
  11.  * <copyright.h>.
  12.  *
  13.  */
  14. /* Amiga port by Tomas Willis (tomas@cae.wisc.edu) January 1995 */
  15.  
  16. #include <stdio.h>
  17. #include "pfs.h"
  18. #include "perrno.h"
  19. #include "pmachine.h"
  20. #include "archie.h"
  21.  
  22. #ifdef NEED_TIME_H
  23. # include <time.h>
  24. # ifdef WANT_BOTH_TIME
  25. #  include <sys/time.h>
  26. # endif
  27. #else
  28. # ifndef VMS
  29. #  include <sys/time.h>
  30. # endif
  31. #endif
  32.  
  33. /* Defined in dirsend.c.  */
  34. extern int client_dirsrv_timeout, client_dirsrv_retry;
  35. /* In archie.c.  */
  36. extern char *program_name;
  37. extern FILE *archie_out;
  38.  
  39. /*
  40.  * Functions defined here
  41.  */
  42. //protos
  43. static void display_link (VLINK l);
  44. static void alex_name (char *host, char *file, int dirp);
  45. static char * alex_reverse (char *string, int len);
  46. void procquery (char *host, char *str, int max_hits, int offset, Query query);
  47. // and elsewhere
  48. extern VLINK archie_query(char *host, char *string, int max_hits,int offset, Query
  49.     query,int (*cmp_proc)(VLINK p, VLINK q),int flags);
  50. extern void atlfree( PATTRIB at);
  51. extern char *month_sname(int n);
  52. extern char * sindex(char *s1, char * s2);
  53.  
  54. /*
  55.  * Data defined here
  56.  */
  57. extern int pwarn, perrno;
  58. extern int listflag, sortflag, alex;
  59.  
  60. #ifdef DEBUG
  61. int pfs_debug;
  62. #endif
  63. static struct tm *presenttime;
  64. static char lastpath[MAX_VPATH] = "\001";
  65. static char lasthost[MAX_VPATH] = "\001";
  66.  
  67. /* Print the contents of the given virtual link.  */
  68. static void
  69. display_link (VLINK l)
  70.  
  71. {
  72.     PATTRIB     ap;
  73.     char    linkpath[MAX_VPATH];
  74.     int        dirflag = 0;
  75. #ifdef MSDOS
  76.     unsigned long size = 0L;
  77. #else
  78.     int        size = 0;
  79. #endif
  80.     char    *modes = "";
  81.     char    archie_date[20];
  82.     char    *gt_date = "";
  83.     int        gt_year = 0;
  84.     int        gt_mon = 0;
  85.     int        gt_day = 0;
  86.     int        gt_hour = 0;
  87.     int        gt_min = 0;
  88.     int        special = listflag || alex;
  89.  
  90.     /* Initialize local buffers */
  91.     *archie_date = '\0';
  92.  
  93.     /* Remember if we're looking at a directory */
  94.     if (sindex(l->type,"DIRECTORY"))
  95.     dirflag = 1;
  96.     else
  97.     dirflag = 0;
  98.     
  99.     /* Extract the linkpath from the filename */
  100.     strcpy(linkpath,l->filename);
  101.     *(linkpath + (strlen(linkpath) - strlen(l->name) - 1)) = '\0';
  102.     
  103.     /* Is this a new host? */
  104.     if (strcmp(l->host,lasthost) != 0) {
  105.     if (!special)
  106.         fprintf (archie_out, "\nHost %s\n\n",l->host);
  107.     strcpy(lasthost,l->host);
  108.     *lastpath = '\001';
  109.     }
  110.     
  111.     /* Is this a new linkpath (location)? */
  112.     if(strcmp(linkpath,lastpath) != 0) {
  113.     if (!special)
  114.         fprintf (archie_out,
  115.              "    Location: %s\n",(*linkpath ? linkpath : "/"));
  116.     strcpy(lastpath,linkpath);
  117.     }
  118.     
  119.     /* Parse the attibutes of this link */
  120.     for (ap = l->lattrib; ap; ap = ap->next) {
  121.     if (strcmp(ap->aname,"SIZE") == 0) {
  122. #ifdef MSDOS
  123.         sscanf(ap->value.ascii,"%lu",&size);
  124. #else
  125.         sscanf(ap->value.ascii,"%d",&size);
  126. #endif
  127.     } else if(strcmp(ap->aname,"UNIX-MODES") == 0) {
  128.         modes = ap->value.ascii;
  129.     } else if(strcmp(ap->aname,"LAST-MODIFIED") == 0) {
  130.         gt_date = ap->value.ascii;
  131.         sscanf(gt_date,"%4d%2d%2d%2d%2d",>_year,
  132.            >_mon, >_day, >_hour, >_min);
  133.         if ((12 * (presenttime->tm_year + 1900 - gt_year) + 
  134.                     presenttime->tm_mon - gt_mon) > 6) 
  135.         sprintf(archie_date,"%s %2d %4d",month_sname(gt_mon),
  136.             gt_day, gt_year);
  137.         else
  138.         sprintf(archie_date,"%s %2d %02d:%02d",month_sname(gt_mon),
  139.              gt_day, gt_hour, gt_min);
  140.     }
  141.     }
  142.     
  143.     /* Print this link's information */
  144.     if (listflag)
  145. #if defined(MSDOS)
  146.       fprintf (archie_out,
  147.            "%s %6lu %s %s%s\n",gt_date,size,l->host,l->filename,
  148.            (dirflag ? "/" : ""));
  149. #else
  150.     fprintf (archie_out,
  151.          "%s %6d %s %s%s\n",gt_date,size,l->host,l->filename,
  152.          (dirflag ? "/" : ""));
  153. #endif
  154.     else if (alex)
  155.       alex_name (l->host, l->filename, dirflag);
  156.     else
  157. #ifdef MSDOS
  158.     fprintf (archie_out,
  159.          "      %9s %s %10lu  %s  %s\n",(dirflag ? "DIRECTORY" : "FILE"),
  160.          modes,size,archie_date,l->name);
  161. #else
  162.     fprintf (archie_out,
  163.          "      %9s %s %10d  %s  %s\n",(dirflag ? "DIRECTORY" : "FILE"),
  164.          modes,size,archie_date,l->name);
  165. #endif /* MSDOS */
  166.  
  167.  
  168.     /* Free the attibutes */
  169.     atlfree(l->lattrib);
  170.     l->lattrib = NULL;
  171. }
  172.  
  173. /*    -    -    -    -    -    -    -    -    */
  174. /*
  175.  * procquery : Process the given query and display the results. If
  176.  *    sortflag is non-zero, then the results are sorted by increasing
  177.  *    date, else by host/filename. If listflag is non-zero then each
  178.  *    entry is printed on a separate, complete line. Note that listflag
  179.  *    is ignored by xarchie.
  180.  */
  181. void
  182. procquery(char *host, char *str, int max_hits, int offset, Query query)
  183. {
  184.     VLINK l;
  185.     long now;
  186.  
  187.     /* initialize data structures for this query */
  188.     (void)time(&now);
  189.     presenttime = localtime(&now);
  190.  
  191.     /* Do the query */
  192.     if (sortflag == 1)
  193.     l = archie_query(host,str,max_hits,offset,query,AQ_INVDATECMP,0);
  194.     else
  195.     l = archie_query(host,str,max_hits,offset,query,NULL,0);
  196.  
  197.     /* Error? */
  198.     if (perrno != PSUCCESS) {
  199.     if (p_err_text[perrno]) {
  200.         if (*p_err_string)
  201.         fprintf(stderr, "%s: failed: %s - %s\n",
  202.             program_name, p_err_text[perrno], p_err_string);
  203.         else
  204.             fprintf(stderr, "%s failed: %s\n",
  205.             program_name, p_err_text[perrno]);
  206.     } else
  207.         fprintf(stderr, "%s failed: Undefined error %d (prospero)",
  208.             program_name, perrno);
  209.     }
  210.  
  211.     /* Warning? */
  212.     if (pwarn != PNOWARN) {
  213.     if (*p_warn_string)
  214.         fprintf(stderr, "%s: Warning! %s - %s\n", program_name,
  215.         p_warn_text[pwarn], p_warn_string);
  216.     else
  217.         fprintf(stderr, "%s: Warning! %s\n", program_name, p_warn_text[pwarn]);
  218.     }
  219.  
  220.  
  221.     /* Display the results */
  222.  
  223.     if (l == (VLINK)NULL && pwarn == PNOWARN && perrno == PSUCCESS) {
  224.     if (! listflag) puts ("No matches.");
  225. #ifdef CUTCP
  226.     netshut();
  227. #endif
  228.     exit (1);
  229.     }
  230.  
  231.     *lasthost = '\001';
  232.     *lastpath = '\001';
  233.     while (l != NULL) {
  234.     display_link (l);
  235.     l = l->next;
  236.     }
  237. }
  238.  
  239. /* Given a dotted hostname, return its Alex root.  */
  240. static char *
  241. alex_reverse (char *string, int len)
  242. //     char *string;
  243. //     int len;
  244. {
  245.   register char *p, *q;
  246.   register int i = 0;
  247.   char *buf = (char *) malloc (len);
  248.  
  249.   p = buf;
  250.   q = (char *) (string + len - 1);
  251.   while (q > string)
  252.     {
  253.       for (i = 0; q > string; q--, i++)
  254.     if (*q == '.')
  255.       {
  256.         q++;
  257.         break;
  258.       }
  259.       if (q == string)
  260.     i++;
  261.       strncpy (p, q, i);
  262.       p += i;
  263.       *p++ = '/';
  264.       i = 0;
  265.       q -= 2;
  266.     }
  267.   *--p = '\0';
  268.   return buf;
  269. }
  270.  
  271. /* Emit a string that's the Alex filename for the given host and file.  */
  272. static void
  273. alex_name (char *host, char *file, int dirp)
  274. {
  275.   int hostlen = strlen (host);
  276.   int len = 6 + hostlen + strlen (file) + dirp +  1;
  277.   char *buf = (char *) malloc (len);
  278.  
  279.   if (! buf)
  280.     exit (99);
  281.  
  282.   sprintf (buf, "/alex/%s%s", alex_reverse (host, hostlen), file);
  283.   if (dirp)
  284.     {
  285.       len -= dirp;
  286.       buf[len - 1] = '/';
  287.       buf[len] = '\0';
  288.     }
  289.  
  290.   fputs (buf, archie_out);
  291. }
  292.